package com.uyayo.ucenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.ucenter.pojo.MemberStatePO;
import com.uyayo.ucenter.entity.MemberState;
import com.uyayo.ucenter.mapper.MemberStateMapper;
import com.uyayo.ucenter.model.MemberStateCondition;
import com.uyayo.ucenter.service.MemberStateService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("memberStateService")
public class MemberStateServiceImpl extends BaseServiceImpl<MemberState> implements MemberStateService {

	private final MemberStateMapper memberStateMapper;

	public MemberStateServiceImpl(MemberStateMapper memberStateMapper) {
		this.memberStateMapper = memberStateMapper;
	}

	@Override
	public List<MemberState> selectMemberState(MemberStateCondition memberStateCondition) {
		Example example = new Example(MemberState.class);
		example.createCriteria();
		return memberStateMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<MemberState> selectMemberStateByPaging(PageBean<MemberState> pageBean,MemberStateCondition memberStateCondition) {

		List<MemberState> userList;
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		
		Example example = new Example(MemberState.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(memberStateCondition.getBeginCreateTime()!=null || memberStateCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", memberStateCondition.getBeginCreateTime(), memberStateCondition.getEndCreateTime());
		}
		userList = memberStateMapper.selectByExample(example);

		//取记录总条数
		PageInfo<MemberState> pageInfo = new PageInfo<>(userList);
		pageBean.setTotalCount(pageInfo.getTotal());
		pageBean.setList(userList);
		return pageBean;
	}


	@Override
	public PageBean<MemberStatePO> selectMemberStateBOByPaging(PageBean<MemberStatePO> pageBean, MemberStateCondition memberStateCondition) {

		List<MemberStatePO> userList;
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());

		Example example = new Example(MemberState.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}

		userList = memberStateMapper.selectMemberStateByExample(memberStateCondition);

		//取记录总条数
		PageInfo<MemberStatePO> pageInfo = new PageInfo<>(userList);
		pageBean.setTotalCount(pageInfo.getTotal());
		pageBean.setList(userList);
		return pageBean;
	}


	@Override
	public int saveMemberState(MemberState memberState) {
		return memberStateMapper.insertSelective(memberState);
	}

	@Override
	public int updateMemberState(MemberState memberState) {
		return memberStateMapper.updateByPrimaryKeySelective(memberState);
	}
}
